﻿using EntityModels.Entities;
using EntityModels.Normal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace EntityModels.DbContexts
{
    /// <summary>
    /// 在一个上下文类型中使用多个模型
    /// </summary>
    public class DynamicModelDbContext : DbContext
    {
        public DbSet<DynamicKeyEntity> MultipleKeyEntities => Set<DynamicKeyEntity>();

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.ReplaceService<IModelCacheKeyFactory, DynamicModelCacheKeyFactory>();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            if (!this.Database.IsSqlServer())
            {
                modelBuilder.Entity<DynamicKeyEntity>()
                    .Property(m => m.Id)
                    .HasConversion<string>();
            }
        }
    }
}
